package com.web;

import java.util.Date;

public class OrderManageAction extends ActionSupport{
	// 依赖注入逻辑组件
	protected IOrderService orderService;
	private Integer id;
	private Order order;
	private String name;
	private String type;
	private String key;
	private Map<String, Object> resultMap = new HashMap<String, Object>();
	
	public void setOrderService(IOrderService orderService) {
		this.orderService = orderService;
	}

	public String addOrder() throws Exception{
		resultMap = new HashMap<String, Object>();
		this.orderService.addOrder(this.name);
		resultMap.put("status", true);
		resultMap.put("msg", "保存成功");
		return SUCCESS;
	}
	
	public String updateOrder() throws Exception{
		resultMap = new HashMap<String, Object>();
		Order order = this.orderService.getOrder(id);
		order.setName(name);
		this.orderService.updateOrder(order);
		resultMap.put("status", true);
		resultMap.put("msg", "保存成功");
		return SUCCESS;
	}

	public String editOrder() throws Exception{
		if(id != null){
			return updateOrder();
		}
		return addOrder();
	}
	
	public String manageOrder() throws Exception{
		if(id != null){
			ActionContext ctx = ActionContext.getContext();
			Order order = this.orderService.getOrder(id);
			String name = order.getName();
			Date addDate = order.getAddDate();
			
			ctx.put("id", id);
			ctx.put("name", name);
			ctx.put("addDate", addDate);
		}
		
		return "manageOrder";
	}

	public String listOrder() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		List<Order> list = orderService.listOrder();
		ctx.put("list", list);
		return "listOrder";
	}
	
	public String delOrder() throws Exception{
		resultMap = new HashMap<String, Object>();
		orderService.delOrder(id);
		resultMap.put("status", true);
		resultMap.put("msg", "删除成功");
		return SUCCESS;
	}
	
	public String orderInfo() throws Exception{
		Order order = orderService.getOrder(id);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("order", order);
		return "showOrder";
	}
	
//前端接口
	/*
	 * 列出所有订单列表
	 */
	public String outwardListOrder() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		List<Order> list = orderService.listOrder();
		
		ctx.put("list", list);
		System.out.println(list);
		return "outwardListOrder";
	}	
	
	/*
	 * 根据关键词列出订单列表
	 * @param type
	 * @param key
	 */
	public String outwardFindOrder() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		List<Order> list;
		if(key == null){
			return "outwardFindOrder";
		}
		key = java.net.URLDecoder.decode(key, "utf-8");
		if(key == null){
			list = orderService.listOrder();
		} else{
			list = orderService.findOrder(key);
		}
		ctx.put("list", list);
		System.out.println(list);
		return "outwardFindOrder";
	}
	
	/*
	 * 订单信息
	 */
	public String showOrder() throws Exception{
		Order order = orderService.getOrder(id);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("order",order);
		return "showOrder";
	}
	/*---------------------------------------*/
	
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

}
